﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SocketServer
{
    internal class GPSListen
    {
        #region 成员变量

        private Socket mySocket;
        private TcpListener lietener;
        private NetworkStream netStream;
        private bool running;
        private Thread thread;

        private byte[] fisrtCom = new byte[4];
        private byte[] listenBuffer;
        private int maxListenBufferSize = 1024;

        private int failCount = 0;

        #endregion

        public GPSListen()
        {
            running = false;
            thread = null;
            listenBuffer = new byte[maxListenBufferSize];
            fisrtCom = new byte[4];

            fisrtCom = Encoding.ASCII.GetBytes("##US");
        }

        #region IListen成员

        /// <summary>
        /// 开始监听
        /// </summary>
        public bool ListenerStart()
        {
            try
            {

                lietener = new TcpListener(IPAddress.Any, 9999);
                lietener.Start();
                thread = new Thread(new ThreadStart(Recieve));
                running = true;
                thread.Start();

                Console.WriteLine("开始监听....");
            }
            catch (Exception err)
            {
                Console.WriteLine("出错："+err.Message);
            }
            return true;
        }

        /// <summary>
        /// 监听结束
        /// </summary>
        public bool ListenStop()
        {
            running = false;
            if (thread != null)
            {
                if (!thread.Join(30000))
                {
                    thread.Abort();
                }
            }

            try
            {
                mySocket.Close();
                lietener.Stop();
            }
            catch (Exception err)
            {
            }
            return true;
        }

        /// <summary>


        #endregion

        private bool IsNewConnecting = false;

        #region 私有方法

        /// <summary>
        /// 线程函数处理命令
        /// </summary>
        /// <param name="com"></param>
        /// <returns></returns>
        private void Recieve()
        {
            while (true)
            {
                try
                {
                    mySocket = lietener.AcceptSocket();
                    Console.WriteLine("接受连接...");
                }
                catch (Exception err)
                {

                }

                if (mySocket.Connected)
                {
                    netStream = new NetworkStream(mySocket, true);
                    IsNewConnecting = true;

                        if (netStream.CanRead && netStream.DataAvailable)
                        {
                            failCount = 0;
                            int readSize = 0;

                            try
                            {
                                readSize = netStream.Read(listenBuffer, 0, maxListenBufferSize);
                            }
                            catch (Exception err)
                            {
                            }
                            //无数据 返回
                            if (readSize <= 0)
                                continue;

                            string msg = Encoding.ASCII.GetString(listenBuffer, 0, readSize);

                            Console.WriteLine(msg);

                    }

                    Thread.Sleep(500);
                }
            }

            #endregion
        }
    }
}
